﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Helper;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System;

namespace CatUtils.Utils.SpecialUtils.Spider
{
    /// <summary>
    /// Selenum的辅助方法
    /// </summary>
    public class SelenumHleper
    {    /*
           请注意 你首先要安装 Selenium.WebDriver  然后安装Selenium.Firefox.WebDriver
           这两个缺一不可，否则会报错。
           */

        private static IWebDriver driver = InitDriver(DriverType.Default, StaticValue.BinImportPath);

        /// <summary>
        /// 选择初始化的浏览器驱动类型
        /// </summary>
        /// <param name="DriverName">调用DriverType的参数即可</param>
        /// <returns></returns>
        public static IWebDriver InitDriver(string DriverName, string DriverPath)
        {
            switch (DriverName)
            {
                case DriverType.Chrome:
                case DriverType.Default:
                    return new ChromeDriver(DriverPath);

                case DriverType.Firefox:

                    return new FirefoxDriver(DriverPath);
            }
            return null;
        }

        public class DriverType

        {
            public const string Firefox = "firefox";
            public const string Chrome = "chrome";
            public const string Default = "default";
        }

        /// <summary>
        /// 获取Iframe的方法和获取HTML的不同，需要额外做
        /// </summary>
        /// <param name="GrabUrl"></param>
        /// <param name="by"></param>
        /// <returns></returns>
        public string GetIframeHtml(string GrabUrl, By by)
        {
            string Shtml = "";
            try
            {
                driver.Navigate().GoToUrl(GrabUrl);
                driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120);
                driver = driver.SwitchTo().Frame(driver.FindElement(by));

                driver.Manage().Window.Minimize();//设置最小化，不影响用户体验。
                Shtml = driver.PageSource;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }

            return Shtml;
        }

        /// <summary>
        /// 要抓取的链接
        /// </summary>
        /// <param name="GrabUrl"></param>
        /// <returns></returns>
        public string GetHtml(string GrabUrl)
        {
            string Shtml = "";
            try
            {
                driver.Navigate().GoToUrl(GrabUrl);
                driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120);

                driver.Manage().Window.Minimize();//设置最小化，不影响用户体验。
                Shtml = driver.PageSource;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }

            return Shtml;
        }

        /// <summary>
        /// 未完成封装的获取cookies方法请注意
        /// </summary>
        /// <param name="GrabUrl"></param>
        /// <returns></returns>
        public string GetCookie(string GrabUrl)
        {
            foreach (Cookie SingleCookie in driver.Manage().Cookies.AllCookies)
            {
                Console.WriteLine(SingleCookie.Domain);
                Console.WriteLine(SingleCookie.Expiry);
                Console.WriteLine(SingleCookie.Name);
                Console.WriteLine(SingleCookie.Secure);
                Console.WriteLine(SingleCookie.Value);
                Console.WriteLine(SingleCookie.Path);
            }
            return "";
        }
    }
}